package com.yyq.gxdc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyq.gxdc.bean.CartVO;
import com.yyq.gxdc.bean.Result;
import com.yyq.gxdc.entity.Cart;
import com.yyq.gxdc.entity.Dish;
import com.yyq.gxdc.entity.User;
import com.yyq.gxdc.service.ICartService;
import com.yyq.gxdc.service.IDishService;
import com.yyq.gxdc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@CrossOrigin
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private ICartService cartService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IDishService dishService;

    /**
     * 分页查找 -- 根据当前登录人获取收货地址
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/front/cartList")
    @ResponseBody
    public Result<IPage<CartVO>> cartList(
            HttpServletRequest request,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10000") Integer pageSize,
            Model model) {

        Result<IPage<CartVO>> result = new Result<IPage<CartVO>>();
        Page<Cart> page = new Page<Cart>(pageNum, pageSize);
        Cart cart = new Cart();
        // 获取当前登录人 信息
        Object curruser = request.getSession().getAttribute("user");
        User user = userService.getById((Integer)curruser);
        cart.setUid(user.getId());
        IPage<CartVO> allCart =  new Page<>();
        IPage<Cart> cartIPage = cartService.getAllCart(page, cart);
        List<Cart> records = cartIPage.getRecords();
        List<CartVO> cartVOList = new ArrayList<>();
        for (Cart cart1:records) {
            CartVO cartVO = new CartVO();
            cartVO.setId(cart1.getId());
            cartVO.setDishId(cart1.getDishId());
            Dish byId = dishService.findById(cart1.getDishId());
            cartVO.setPrice(byId.getPrice());
            cartVO.setDishName(byId.getName());
            cartVO.setNumber(cart1.getNumber());
            cartVO.setRimage(byId.getRimage());
            cartVOList.add(cartVO);
        }
        allCart.setRecords(cartVOList);
        result.setSuccess(true);
        result.setResult(allCart);
        return result;
    }


    /**
     * 添加商品到购物车管理信息
     * @param cart
     * @param req
     * @return
     */
    @PostMapping(value = "/addCart")
    @ResponseBody
    public Result<Object> addCart(Cart cart, HttpServletRequest req) {
        // 新增 地址信息
        Object curruser = req.getSession().getAttribute("user");
        if(curruser == null){
            return Result.error("请先登录！");
        }
        User user = userService.getById((Integer)curruser);
        cart.setUid(user.getId());
        cart.setNumber(1);
        // 判断这个商品是不是加过购物车了
        Cart byUidAndDishId = cartService.findByUidAndDishId(user.getId(), cart.getDishId());
        if(Objects.isNull(byUidAndDishId)){
            cartService.add(cart);
        }else{
            byUidAndDishId.setNumber(byUidAndDishId.getNumber()+1);
            cartService.update(cart);
        }


        return Result.ok();
    }

    /**
     * 删除购物车商品
     * @param cart
     * @param req
     * @return
     */
    @PostMapping(value = "/deleteCart")
    @ResponseBody
    public Result<Object> deleteCart(Cart cart, HttpServletRequest req) {
        // 先查询 这个 购物车记录 是否已存在
        Cart byId = cartService.findById(cart.getId());
        if(byId == null ){
            return Result.error("该地址不存在！");
        }else{
            cartService.delete(cart.getId());
            return Result.ok();
        }
    }

    /**
     * 修改地址
     * @param cart
     * @param req
     * @return
     */
    @PostMapping(value = "/updateCart")
    @ResponseBody
    public Result<Object> updateCart(Cart cart, HttpServletRequest req) {
        // 先查询 这个购物车记录是否已存在
        Cart byId = cartService.findById(cart.getId());
        if(byId == null ){
            return Result.error("该菜品不存在！");
        }else{
            cartService.update(cart);
        }
            return Result.ok();
    }

}
